package com.haois.component.entity;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.internal.LinkedTreeMap;
import com.haois.component.common.enu.ApiResultEnum;

import java.util.List;
import java.util.Map;

/**
 * description
 *
 * @author RuiHaoZ on GCU
 * @date 2017/3/17 9:18
 */
public class Reply<T> {
    private String code; // 0 返回码
    private String msg;//具体信息
    private T data;//返回需要的数据
    private String dataType;

    public Reply(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public Reply(ApiResultEnum apiCode) {
        this.code = apiCode.getCode();
        this.msg = apiCode.getMsg();
    }

   /* public Reply() {
        this.code = ApiResultEnum.API_00.getCode();
        this.msg = ApiResultEnum.API_00.getMsg();
    }*/

    public Reply(ApiResultEnum apiCode, T data) {
        this.code = apiCode.getCode();
        this.msg = apiCode.getMsg();
        this.data = data;
        if (data != null) {
            this.dataType = data.getClass().getName();
        }
    }

    public Reply(String code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        if (data != null) {
            this.dataType = data.getClass().getName();
        }
    }


    public Reply(String code, String msg, T data, String dataType) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.dataType = dataType;
    }

    public Reply(ApiResultEnum apiCode, T data, String dataType) {
        this.code = apiCode.getCode();
        this.msg = apiCode.getMsg();
        this.data = data;
        this.dataType = dataType;
    }

    public String toJsonNotNull() {
        Gson gson = new Gson();
        return gson.toJson(this);
    }

    public String toJson() {
        Gson gson = new GsonBuilder().serializeNulls().create();
        return gson.toJson(this).replaceAll("null", "\"\"");
    }

    public static String createReply(String code, String msg) {
        return new Reply(code, msg).toJson();
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getDataType() {
        return dataType;
    }

    public void setDataType(String dataType) {
        this.dataType = dataType;
    }


    public Reply dataIsEmpty() {
        Object object = this.data;
        if (object == null) {
            this.msg = ApiResultEnum.API_02.getMsg();
            this.code = ApiResultEnum.API_02.getCode();
        } else {
            if (object instanceof List) {
                List list = (List) object;
                if (list.size() == 0) {
                    this.msg = ApiResultEnum.API_01.getMsg();
                    this.code = ApiResultEnum.API_01.getMsg();
                }
            }
        }
        return this;
    }

    /**
     * @api {GET} /api  生成返回数据
     * @apiGroup apiReply
     * @apiName getApiMsg
     * @apiDescription 请求返回数据 0XX为正常返回，1XX为异常返回，未知错误为服务抛出未知异常
     * @apiSuccess (200) {String} msg 信息
     * @apiSuccess (200) {int} code  0xx:正常返回/1xx:异常返回
     * @apiSuccess (200) {T} data 获取的真实数据
     * @apiSuccessExample {json} 返回样例:
     * {"code":"00","msg":"NODE ONE","data":[{"code":"00","msg":"正常响应"},{"code":"01","msg":"查询为空"},{"code":"02","msg":"没有对应数据"},{"code":"03","msg":"违反唯一性"},{"code":"04","msg":"没有权限"},{"code":"05","msg":"缺省值"},{"code":"10","msg":"普通错误"},{"code":"11","msg":"系统内部错误"},{"code":"12","msg":"参数传递不正确"},{"code":"13","msg":"非法请求"},{"code":"15","msg":"缺省值"}]}
     */
    public static List<Reply> getApiMsg() {
        return null;
    }


    private static Reply getBaseReply(Map result) {
        String code = (String) result.get("code");
        String msg = (String) result.get("msg");
        return new Reply(code, msg);
    }

    public static Reply getReplyList(Map result) {
        Reply reply = getBaseReply(result);
        List<LinkedTreeMap> list = (List) result.get("data");
        reply.setData(list);
        return reply;
    }

    public static Reply getReply(Map result) {
        Reply reply = getBaseReply(result);
        LinkedTreeMap linkedTreeMap = (LinkedTreeMap) result.get("data");
        reply.setData(linkedTreeMap);
        return reply;
    }
}
